// 声明全局uni对象
declare const uni: any

// 户型查询参数接口
export interface HouseTypeParams {
  roomType?: string  // 户型类型(如"一室"、"二室")
  pageNum?: number // 页码
  pageSize?: number // 每页大小
}

// 户型数据接口 - 与后端返回字段保持一致
export interface HouseTypeInfo {
  id: number
  roomType: string // 户型类型：几室几厅
  roomCount: number // 房间数
  area: string // 面积
  housePic: string // 户型图地址
}

// 接口返回值类型
interface ResponseData<T> {
  code: number
  msg: string
  rows: T[]
  total: number
}

// 户型接口服务
export const houseTypeApi = {
  // 获取户型列表
  getHouseTypes(params: HouseTypeParams = {}) {
    return new Promise<{data?: {rows: HouseTypeInfo[], total: number}, statusCode: number}>((resolve, reject) => {
      uni.request({
        url: 'http://localhost:9204/housesystem/list',
        method: 'GET',
        data: params,
        success: (res: any) => {
          if (res.statusCode === 200) {
            resolve({
              data: {
                rows: res.data.rows || [],
                total: res.data.total || 0
              },
              statusCode: 200
            })
          } else {
            resolve({
              statusCode: res.statusCode,
              data: undefined
            })
          }
        },
        fail: () => {
          reject(new Error('网络请求失败'))
        }
      })
    })
  },
  
  // 获取户型详情
  getHouseTypeDetail(id: number) {
    return new Promise<{data?: HouseTypeInfo, statusCode: number}>((resolve, reject) => {
      uni.request({
        url: `http://localhost:9204/housesystem/${id}`,
        method: 'GET',
        success: (res: any) => {
          if (res.statusCode === 200) {
            resolve({
              data: res.data.data,
              statusCode: 200
            })
          } else {
            resolve({
              statusCode: res.statusCode,
              data: undefined
            })
          }
        },
        fail: () => {
          reject(new Error('网络请求失败'))
        }
      })
    })
  }
} 